package com.spbdx.web.controller.system;

import com.spbdx.common.annotation.Log;
import com.spbdx.common.core.controller.BaseController;
import com.spbdx.common.core.domain.AjaxResult;
import com.spbdx.common.core.page.TableDataInfo;
import com.spbdx.common.enums.BusinessType;
import com.spbdx.system.domain.SysNotice;
import com.spbdx.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 咨询信息处理
 * 
 * @author cms
 */
@RestController
@Api("资讯管理")
@RequestMapping("/system/news")
public class SysNewsController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知咨询列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    @ApiOperation("获取资讯列表")
    public TableDataInfo list(SysNotice news)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(news);
        return getDataTable(list);
    }

    /**
     * 根据咨询编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{newsId}")
    @ApiOperation("获取资讯详情")
    public AjaxResult getInfo(@PathVariable Long newsId)
    {
        return success(noticeService.selectNoticeById(newsId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @ApiOperation("新增资讯")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice news)
    {
        news.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(news));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改资讯")
    public AjaxResult edit(@Validated @RequestBody SysNotice news)
    {
        news.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(news));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    @ApiOperation("批量删除资讯")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(newsIds));
    }
}
